﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WMM.Tapioca.Taxonomy;
using WMM.Tapioca.Validate;
using WMM.Tapioca.Instance;
using System.Diagnostics;
using WMM.Tapioca.Formula;

namespace WMM.Tapioca
{
    /// <summary>
    /// XbrlManager provides an interface between InstanceDocuments's and the controller.  Xbrl manager is a 
    /// singleton object, one is created in initialization of a controller.
    /// </summary>
    public class XbrlManager : Controller
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        private DTS dts;
        private InstanceDocument instance;

        public bool IsValidateDTS = true;
        public bool IsValidateCalculation = true;
        public bool IsValidateFormula = true;

        //public XbrlManager()
        //{
        //}

        /// <summary>
        /// Constructor
        /// </summary>
        public XbrlManager(InstanceDocument xbrlInstance)
        {
            if (xbrlInstance != null)
            {
                dts = xbrlInstance.Dts;
                instance = xbrlInstance;
            }
        }

        /// <summary>
        /// Method that to validateXBRL instances, XBRL linkbases and XBRL taxonomy schemas MUST comply with
        /// the syntax requirements imposed in this specification.
        /// </summary>
        public void Validate()
        {
            //DateTime startTime = DateTime.Now;
            Stopwatch stopWatch = Stopwatch.StartNew();

            if (IsValidateDTS)
            {
                ValidateTaxonomy();
            }            

            ValidateInstance vi = new ValidateInstance(instance); // ValidateDimensionalPrimaryItem

            if (IsValidateCalculation)
            {
                ValidateCalculations vc = new ValidateCalculations(dts, false);
            }            

            if (IsValidateFormula)
            {
                FormulaProcessor vf = new FormulaProcessor(dts, false);
            }

            stopWatch.Stop();
            // Get the elapsed time as a TimeSpan value.
            TimeSpan ts = stopWatch.Elapsed;

            log.Info("Processing time of validation:" + ts.ToString(@"mm\:ss\.fff") + "\n\n");
        }


        public void ValidateTaxonomy()
        {
            ValidateDts vDts = new ValidateDts(dts);

            ValidateLinkbases vlb = new ValidateLinkbases(dts);

            ValidateDimensionalTaxonomy vd = new ValidateDimensionalTaxonomy(dts);

            ValidateLinkbaseFormula vlf = new ValidateLinkbaseFormula(dts);
        }
    }
}
